/// <reference path="_libs\Xrm2011.d.ts" />
/// <reference path="_libs\Microsoft.Xrm.Sdk.d.ts" />

/* To use this sample project:
    1. Add Microsoft.Xrm.Sdk.js and app.js as web resources
    2. Add both as form libraries or if adding to ribbon add javascript functions. Make sure to call isNaN on Microsoft.Xrm.Sdk to load it before app.js
    3. Call XrmSdk.Samples.Runner.RunDemo function
*/
module XrmSdk.Samples {
    export class Runner {
        static RunDemo() {

            new XrmSdk.Samples.Runner().Run();
        }

        private _sdk: $Xrm.OrganizationService;

        constructor() {
            // var url = Xrm.Page.context.getClientUrl(); // RU12 and above
            var url = Xrm.Page.context.getServerUrl();
            this._sdk = new $Xrm.OrganizationService(Runner.GetCorrectedServerUrl(url));
        }

        Run() {
            var accounts = this.GetAccounts();
            alert('Found ' + accounts.length + ' accounts');

            for (var i = 0; i < accounts.length; i++) {
                var current = accounts[i];
                var name = current.GetAttributeValue("name");

                this.UpdateAccount(i, current);
                alert(name + ' updated, click OK to test de-activation');

                this.Deactivate(current);
                alert(name + ' de-activated, click OK to test activation');

                this.Activate(current);
                alert(name + ' activated');

                break; //
            }            
        }

        GetAccounts(): $Xrm.Entity[] {
            var fetch = "<fetch><entity name='account'><attribute name='accountid'/><attribute name='name'/></entity></fetch>";

            return this._sdk.RetrieveMultiple(fetch);
        }

            UpdateAccount(i: number, account: $Xrm.Entity): void {
            account["name"] = account.GetAttributeValue("name") + " : " + i;

            this._sdk.Update(account);
        }

            Activate(account: $Xrm.Entity) {
                var request = new $Xrm.Messages.SetStateRequest();
                request.EntityMoniker = new $Xrm.EntityReference("account", account.Id);
                request.State = new $Xrm.OptionSetValue(0);
                request.Status = new $Xrm.OptionSetValue(-1);

            this._sdk.Execute(request);
        }

            Deactivate(account: $Xrm.Entity) {
                var request = new $Xrm.Messages.SetStateRequest();
                request.EntityMoniker = new $Xrm.EntityReference("account", account.Id);
                request.State = new $Xrm.OptionSetValue(1);
                request.Status = new $Xrm.OptionSetValue(-1);

            this._sdk.Execute(request);
        }

        private static GetCorrectedServerUrl(sdkUrl: string): string {
            var partsWindow = window.location.href.split('/');
            var partsSdk = sdkUrl.split('/');
            var correctedUrl = '';
            for (var i = 0; i < partsSdk.length; i++) {
                if (i < 3) {
                    correctedUrl += partsWindow[i] + '/';
                }
                else {
                    correctedUrl += partsSdk[i] + '/';
                }
            }
            return correctedUrl;
        }
    }
}